/*
 * @Author: Choisaaaa
 * @Date: 2018/5/11  0:02
 * 卖家出售的商品列表
 */

require("./seller-product-list.css");
var nav = require("page/common/nav/nav.js");
require("page/common/header/header.js");
var _mm = require("util/mm.js");
var navSide = require("page/common/nav-side/nav-side.js");              //侧边导航模块
var _product = require("service/product-service.js");                   //商品的数据请求
var Pagination = require("util/pagination/pagination.js");/* 导入分页模块 */
var _user = require("service/user-service.js");
var templateIndex = require("./seller-product-list.string");

var page = {
    data : {
        status : 0,
        productName : "",
        type : "list",
        pageNum : 1,
        pageSize : 10,
        userId : 0
    },
    proData : { //缓存商品统计信息

    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function () {
        this.onLoadSide();              //加载侧边导航
        this.onLoadProductList();
        this.onLoadProData(); //加载商品数据统计信息
    },
    onLoadProData : function () {
        var _this = this;
        _product.getProData(function (res) {
            _this.proData = res.data
        }, function () {
            
        });
    },
    bindEvent : function () {
        this.onClickOperation(); //更新商品的状态
        this.onChangeSelect();//点击下拉框
        this.onClickSearch();//点击搜索按钮
        this.clickFooterCell();//点击尾部的状态
    },
    clickFooterCell : function () {
        var _this = this;
      $(document).on("click",".product-footer .pro-cell", function () {
          var status = $(this).data("value");
          if(status || status ==0){
              _this.data.status = status;
              _this.getSearchData();
          }
      });
    },
    onClickSearch : function () {
        var _this = this;
        $(document).on("click","#search-pro", function () {
            var productName = $("#pro-name").val();
            _this.data.productName = productName;//缓存商品名称，点击分页重新加载需要用到
            _this.data.status = "";//按商品名称搜索时先将商品的状态重置为空
            var userId = _this.data.userId;
            if(productName && userId){
                _this.getSearchData();
            }else{
                _mm.errorTips("请输入需要商品名称");
            }
        });
    },
    onChangeSelect : function () {
        var _this = this;
        $(document).on("change","#pro-select", function () {
            var status = $(this).val();
            _this.data.status = status;
            _this.getSearchData();
        });
    },
    getSearchData : function () {
        var status = this.data.status;
        var userId = this.data.userId;
        if(this.data.productName){ //非空判断
            var productName = this.data.productName;
        }
        var _this = this;
        _product.searchProducts({
            productName : productName,
            status : status,
            ownerId : userId,
            pageNum : this.data.pageNum,
            pageSize : this.data.pageSize
        }, function (res) {
            _this.data.pageNum = 1;//重置页数，防止数据冗余
            _this.data.type = "search"
            _this.onLoadProductInfo(res);
        }, function (err) {
            _mm.errorTips(err.msg);
        });


    },
    onClickOperation : function () {
        var _this = this;
        $(document).on("click","#operation", function () {
            var oldStatus = $(this).attr("value");
            var newStatus;
            var tips ;
            var productId = $(this).parents(".pro-table").data("product-id");
            if(oldStatus == "-1"){
                newStatus = 1;
                tips = "确定要上架该上商品吗？"
            }else  if(oldStatus == "2"){
                newStatus = -1;
                tips = "确定要下架该上商品吗？"
            }else {
                return;
            }
            if(window.confirm(tips) && newStatus && productId){
                _product.updateProductStatus({
                    productId : productId,
                    status : newStatus
                }, function (res) {
                    _mm.successTips(res.data);
                    _this.onLoadProductList();
                }, function (err) {
                    _mm.errorTips(err.msg)
                })
            }


        });
    },

    /** 1.初始化加载页面时的数据渲染 **/
    onLoadSide : function(){ //1.1:初始化侧边导航
        navSide.init({
            name : "seller-center"
        });
    },
    onLoadProductList : function () {
        if(this.data.type == "list"){
            var _this  = this,
                userId = _this.data.userId
            if(userId === 0) {
                _user.checkLogin(function (res) {
                    _this.data.userId = res.data.userId;//缓存用户ID
                    _this.getProductList( _this.data.userId);
                }, function (err) {
                    _mm.doLogin();
                });
            }else{
                this.getProductList( _this.data.userId);
            }
        }else if(this.data.type == "search"){
            this.getSearchData();
        }
    },
    getProductList : function (userId) {
        var _this = this;
        _product.getSellerProductList({
            ownerId : parseInt(userId),
            pageNum : _this.data.pageNum,
            pageSize : _this.data.pageSize
        }, function (res) {
            _this.data.pageNum = 1;//重置页数，防止数据冗余
            _this.onLoadProductInfo(res);
        }, function (err) {
            _mm.errorTips(err.msg)
        });
    },
    onLoadProductInfo : function (res) {
        var _this = this;
        res.data = $.extend({},res.data,_this.proData); //将得到的数据和商品数据统计信息合并为一个对象
        if(res.data.list.length > 0){
            var footer = true;
            res.data.list.map(function (product) {
                var opration = "";
                if(product.enableStatus == 2){ //根据商品的状态追加一个操作属性
                    opration = "去下架";
                }else if(product.enableStatus == -1){
                    opration = "去上架";
                }else{
                    opration = "无权操作"
                }
                product.operation = opration;
            });
        }
        var productListHtml = _mm.renderHtml(templateIndex,{
            footer : footer,
            data : res.data
        });
        $(".product-list").html(productListHtml);
        _this.loadPagination({//加载分页信息
            hasPreviousPage : res.data.hasPreviousPage,//是否有上一页，boolean
            prePage         : res.data.prePage,//上一页的编号
            hasNextPage     : res.data.hasNextPage,//是否有上一页，boolean
            nextPage        : res.data.nextPage,//下一页的编号
            pageNum         : res.data.pageNum,//当前的页号
            pages           : res.data.pages//总的页数
        });

    },
    //3.辅助函数
    loadPagination : function(pageInfo){//3.1分页
        var _this = this;
        this.pagination ? "" : (this.pagination = new Pagination());
        this.pagination.render($.extend({},pageInfo,{
            container : $(".pagination"),
            onSelectPage : function(pageNum){
                _this.data.pageNum = pageNum; //保存从分页组件中点击的页数
                _this.onLoadProductList();
            }
        }));
    },
}
$(function () {
    page.init();
});